package com.ruoyi.project.business.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.project.business.mapper.QuestionMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.business.domain.Question;
import com.ruoyi.project.business.service.IQuestionService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 题库管理Controller
 *
 * @author hongwj
 * @date 2024-09-26
 */
@RestController
@RequestMapping("/business/question")
@Slf4j
public class QuestionController extends BaseController {
    @Autowired
    private IQuestionService questionService;


    @GetMapping("/all/questionss")
    public AjaxResult all(@RequestParam(value = "ids", required = false) String idsJson) {
        try {
            List<Question> list;
            if (idsJson != null && !idsJson.isEmpty()) {
                ObjectMapper objectMapper = new ObjectMapper();
                List<Long> ids = Arrays.asList(objectMapper.readValue(idsJson, Long[].class));
                list = questionService.selectQuestionsByIds(ids);
            } else {
                list = questionService.selectQuestionList(new Question());
            }
            return success(list);
        } catch (Exception e) {
            log.error("Error processing request", e);
            return error("Error processing request");
        }
    }

    /**
     * 查询题库管理列表
     */
    //@PreAuthorize("@ss.hasPermi('business:question:list')")
    @GetMapping("/list")
    public TableDataInfo list(Question question) {
        startPage();
        List<Question> list = questionService.selectQuestionList(question);
        return getDataTable(list);
    }

    /**
     * 查询所有题库管理列表
     */
    //@PreAuthorize("@ss.hasPermi('business:question:all')")
    @Log(title = "题库管理", businessType = BusinessType.OTHER)
    @GetMapping("/all/questions")
    public AjaxResult all(Question question) {
        List<Question> list = questionService.selectQuestionList(question);
        return success(list);
    }

    @Autowired
    private QuestionMapper questionMapper;

    /**
     * 查询所有题库管理列表
     */
    @Log(title = "题库管理", businessType = BusinessType.OTHER)
    @GetMapping("/selectQuestionRandomList")
    public AjaxResult selectQuestionRandomList(Question question) {
        List<Question> list = questionMapper.selectQuestionRandomList(question);
        return success(list);
    }

    /**
     * 导出题库管理列表
     */
    //@PreAuthorize("@ss.hasPermi('business:question:export')")
    @Log(title = "题库管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Question question) {
        List<Question> list = questionService.selectQuestionList(question);
        ExcelUtil<Question> util = new ExcelUtil<Question>(Question.class);
        util.exportExcel(response, list, "题库管理数据");
    }

    /**
     * 获取题库管理详细信息
     */
    //@PreAuthorize("@ss.hasPermi('business:question:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(questionService.selectQuestionById(id));
    }

    /**
     * 新增题库管理
     */
    //@PreAuthorize("@ss.hasPermi('business:question:add')")
    @Log(title = "题库管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Question question) {
        return toAjax(questionService.insertQuestion(question));
    }

    /**
     * 修改题库管理
     */
    //@PreAuthorize("@ss.hasPermi('business:question:edit')")
    @Log(title = "题库管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Question question) {
        return toAjax(questionService.updateQuestion(question));
    }

    /**
     * 删除题库管理
     */
    //@PreAuthorize("@ss.hasPermi('business:question:remove')")
    @Log(title = "题库管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(questionService.deleteQuestionByIds(ids));
    }
}
